﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml.Linq;
using NextCMS.Models;

namespace NextCMS.DataAccess.XMLDataAccess
{
    internal class Pages : IPage
    {
        private string FilePath = HttpContext.Current.Server.MapPath("~") + "/App_Data/Pages.xml";
        private string FilePath1 = HttpContext.Current.Server.MapPath("~") + "/App_Data/PageInfoByLanguage.xml";
        XDocument docPages = null;
        XDocument docPageInfoByLang = null;
        public Pages()
        {
            docPages = XDocument.Load(FilePath);  //load the xml file.
            docPageInfoByLang = XDocument.Load(FilePath1); 
        }
        public PageModel GetByPageId(int id)
        {
            var q = from d in docPages.Descendants("Page")
                    where (int)d.Element("Id") == id
                    select new PageModel
                    {
                        Id = (int)d.Element("Id"),
                        SiteId = (int)d.Element("SiteId"),
                        FileName = d.Element("FileName").Value
                    };
            return q.FirstOrDefault();
        }
        public PageModel GetByPageLanguageId(int id)
        {

            var q = from d in docPages.Descendants("Page")
                    join pd in docPageInfoByLang.Descendants("PageInfoByLanguage")
                    on (int)d.Element("Id") equals (int)pd.Element("PageId")
                    where (int)pd.Element("Id") == id
                    select new PageModel
                    {
                        Id = (int)d.Element("Id"),
                        SiteId = (int)d.Element("SiteId"),
                        FileName = d.Element("FileName").Value,
                        LanguageId = (int)pd.Element("LanguageId"),
                        PageLanguageId = (int)pd.Element("Id"),
                        MasterPageId = (int)pd.Element("MasterPageId"),
                        Title = pd.Element("Title").Value,
                        MetaKeywords = pd.Element("MetaKeywords").Value,
                        MetaDescription = pd.Element("MetaDescription").Value
                    };
            return q.FirstOrDefault();
        }
        public PageModel GetByPageIdLanguageId(int PageId,int LanguageId)
        {

            var q = from d in docPages.Descendants("Page")
                    join pd in docPageInfoByLang.Descendants("PageInfoByLanguage")
                    on (int)d.Element("Id") equals (int)pd.Element("PageId")
                    where (int)d.Element("Id") == PageId && (int)pd.Element("LanguageId") == LanguageId
                    select new PageModel
                    {
                        Id = (int)d.Element("Id"),
                        SiteId = (int)d.Element("SiteId"),
                        FileName = d.Element("FileName").Value,
                        LanguageId = (int)pd.Element("LanguageId"),
                        PageLanguageId = (int)pd.Element("Id"),
                        MasterPageId = (int)pd.Element("MasterPageId"),
                        Title = pd.Element("Title").Value,
                        MetaKeywords = pd.Element("MetaKeywords").Value,
                        MetaDescription = pd.Element("MetaDescription").Value
                    };
            return q.FirstOrDefault();
        }
        public IEnumerable<PageModel> GetByMasterPageId(int id)
        {
            var q = from d in docPages.Descendants("Page")
                    join pd in docPageInfoByLang.Descendants("PageInfoByLanguage")
                    on (int)d.Element("Id") equals (int)pd.Element("PageId")
                    where (int)pd.Element("MasterPageId") == id
                    select new PageModel
                    {
                        Id = (int)d.Element("Id"),
                        SiteId = (int)d.Element("SiteId"),
                        FileName = d.Element("FileName").Value,
                        LanguageId = (int)pd.Element("LanguageId"),
                        MasterPageId = (int)pd.Element("MasterPageId"),
                        Title = pd.Element("Title").Value,
                        MetaKeywords = pd.Element("MetaKeywords").Value,
                        MetaDescription = pd.Element("MetaDescription").Value
                    };
            return q;
        }
        public IEnumerable<PageModel> GetAllBySiteId(int SiteId)
        {

            var q = from d in docPages.Descendants("Page")
                    where (int)d.Element("SiteId") == SiteId
                    select new PageModel
                    {
                        Id = (int)d.Element("Id"),
                        SiteId = (int)d.Element("SiteId"),
                        FileName = d.Element("FileName").Value,
                        LanguageId = 0,
                        MasterPageId = 0,
                        Title = "",
                        MetaKeywords = "",
                        MetaDescription = ""
                    };
            return q;
        }
        public IEnumerable<PageModel> GetAllBySiteIdLanguageId(int SiteId, int LanguageId)
        {

            var q = from d in docPages.Descendants("Page")
                    join pd in docPageInfoByLang.Descendants("PageInfoByLanguage")
                    on (int)d.Element("Id") equals (int)pd.Element("PageId") into pageLang
                    from pl in pageLang.DefaultIfEmpty()
                    where (int)d.Element("SiteId") == SiteId && (pl.Element("LanguageId") == null ? 0 : (int)(pl.Element("LanguageId"))) == LanguageId
                    select new PageModel
                    {
                        Id = (int)d.Element("Id"),
                        SiteId = (int)d.Element("SiteId"),
                        FileName = d.Element("FileName").Value,
                        LanguageId = LanguageId,
                        PageLanguageId = pl.Element("Id") == null ? 0 : (int)pl.Element("Id"),
                        MasterPageId = pl.Element("MasterPageId") == null ? 0 : (int)pl.Element("MasterPageId"),
                        Title = pl.Element("Title")!=null?pl.Element("Title").Value:"",
                        MetaKeywords = pl.Element("MetaKeywords")!=null ? pl.Element("MetaKeywords").Value:"",
                        MetaDescription = pl.Element("MetaDescription")!=null ? pl.Element("MetaDescription").Value:""
                    };
            return q;
        }
        public PageModel GetPageByFileName(string fileName, int SiteId,int UserLanguageId)
        {
            var q = from d in docPages.Descendants("Page")
                    join pd in docPageInfoByLang.Descendants("PageInfoByLanguage")
                    on (int)d.Element("Id") equals (int)pd.Element("PageId")
                    where d.Element("FileName").Value.ToLower() == fileName.ToLower() && (int)d.Element("SiteId") == SiteId
                    && (int)pd.Element("LanguageId")==UserLanguageId
                    select new PageModel
                    {
                        Id = (int)d.Element("Id"),
                        SiteId = (int)d.Element("SiteId"),
                        FileName = d.Element("FileName").Value,
                        LanguageId = (int)pd.Element("LanguageId"),
                        MasterPageId = (int)pd.Element("MasterPageId"),
                        Title = pd.Element("Title").Value,
                        MetaKeywords = pd.Element("MetaKeywords").Value,
                        MetaDescription = pd.Element("MetaDescription").Value
                    };
            
            return q.FirstOrDefault();
        }
        public PageModel GetPageByFileName(string fileName, int Id)
        {
            var q = from d in docPages.Descendants("Page")
                    join pd in docPageInfoByLang.Descendants("PageInfoByLanguage")
                    on (int)d.Element("Id") equals (int)pd.Element("PageId")
                    where d.Element("FileName").Value.ToLower() == fileName.ToLower() && (int)d.Element("Id") != Id
                    select new PageModel
                    {
                        Id = (int)d.Element("Id"),
                        SiteId = (int)d.Element("SiteId"),
                        FileName = d.Element("FileName").Value,
                        LanguageId = (int)pd.Element("LanguageId"),
                        MasterPageId = (int)pd.Element("MasterPageId"),
                        Title = pd.Element("Title").Value,
                        MetaKeywords = pd.Element("MetaKeywords").Value,
                        MetaDescription = pd.Element("MetaDescription").Value
                    };
            
            return q.FirstOrDefault();
        }
        public int Save(PageModel page)
        {

            var q = from d in docPages.Descendants("Page")
                    select new PageModel
                    {
                        Id = (int)d.Element("Id"),
                    };
            if (q.Count() > 0)
                page.Id = q.Max(s => s.Id) + 1;
            else
                page.Id = 1;

            var oMember = new XElement("Page",
             new XElement("Id", page.Id),
             new XElement("SiteId", page.SiteId),
             new XElement("FileName", page.FileName)
             
             );
            docPages.Root.Add(oMember);
            docPages.Save(FilePath);


            //Add PageInfor
            int pageInfoId = 0;
            var q1 = from d in docPageInfoByLang.Descendants("PageInfoByLanguage")
                    select new PageModel
                    {
                        Id = (int)d.Element("Id"),
                    };
            if (q1.Count() > 0)
                pageInfoId = q1.Max(s => s.Id) + 1;
            else
                pageInfoId = 1;


            var oMember1 = new XElement("PageInfoByLanguage",
             new XElement("Id", pageInfoId),
             new XElement("PageId", page.Id),
             new XElement("LanguageId", page.LanguageId),
             new XElement("MasterPageId", page.MasterPageId),
             new XElement("Title", page.Title),
             new XElement("MetaKeywords", page.MetaKeywords),
             new XElement("MetaDescription", page.MetaDescription)
             );
            docPageInfoByLang.Root.Add(oMember1);
            docPageInfoByLang.Save(FilePath1);

            return page.Id;
        }
        // Delete Record
        public void Delete(int id)
        {
            docPageInfoByLang.Root.Elements("PageInfoByLanguage").Where(i => (int)i.Element("PageId") == id).Remove();
            docPageInfoByLang.Save(FilePath1);

            docPages.Root.Elements("Page").Where(i => (int)i.Element("Id") == id).Remove();
            docPages.Save(FilePath);
        }

        // Edit Record
        public void Edit(PageModel page)
        {

            XElement node = docPages.Root.Elements("Page").Where(i => (int)i.Element("Id") == page.Id).FirstOrDefault();
            node.SetElementValue("FileName", page.FileName);
            docPages.Save(FilePath);


            if (page.PageLanguageId > 0)
            {
                XElement node1 = docPageInfoByLang.Root.Elements("PageInfoByLanguage").Where(i => (int)i.Element("Id") == page.PageLanguageId).FirstOrDefault();
                node1.SetElementValue("MasterPageId", page.MasterPageId);
                node1.SetElementValue("Title", page.Title);
                node1.SetElementValue("MetaKeywords", page.MetaKeywords);
                node1.SetElementValue("MetaDescription", page.MetaDescription);
                docPageInfoByLang.Save(FilePath1);
            }
            else
            {
                //Add PageInfor
                int pageInfoId = 0;
                var q1 = from d in docPageInfoByLang.Descendants("PageInfoByLanguage")
                         select new PageModel
                         {
                             Id = (int)d.Element("Id"),
                         };
                if (q1.Count() > 0)
                    pageInfoId = q1.Max(s => s.Id) + 1;
                else
                    pageInfoId = 1;


                var oMember1 = new XElement("PageInfoByLanguage",
                 new XElement("Id", pageInfoId),
                 new XElement("PageId", page.Id),
                 new XElement("LanguageId", page.LanguageId),
                 new XElement("MasterPageId", page.MasterPageId),
                 new XElement("Title", page.Title),
                 new XElement("MetaKeywords", page.MetaKeywords),
                 new XElement("MetaDescription", page.MetaDescription)
                 );
                docPageInfoByLang.Root.Add(oMember1);
                docPageInfoByLang.Save(FilePath1);
            }
            
        }

        public void Dispose()
        {
            docPages = null;
        }
    }
}
